package service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import security.MySecurityContextHandle;
import service.interf.IAgentBaseConfig;

import common.impl.exception.ServiceException;

import dao.interf.IAgentBaseConfigDAO;
import dao.interf.IAgentBaseDefDAO;
import dao.interf.IIfwAgentRegInfoDAO;
import dao.pojo.AgentBaseConfig;
import dao.pojo.AgentBaseDef;
import dao.pojo.IfwAgentRegInfo;

public class AgentBaseConfigImpl extends BaseDataServiceImpl implements
		IAgentBaseConfig {

	private IAgentBaseConfigDAO agentBaseConfigDao;
	private IIfwAgentRegInfoDAO agentRegInfoDao;
	private IAgentBaseDefDAO agentBaseDefDao;

	public void setAgentBaseConfigDao(IAgentBaseConfigDAO agentBaseConfigDao) {
		this.agentBaseConfigDao = agentBaseConfigDao;
	}

	public void setAgentRegInfoDao(IIfwAgentRegInfoDAO agentRegInfoDao) {
		this.agentRegInfoDao = agentRegInfoDao;
	}

	public void setAgentBaseDefDao(IAgentBaseDefDAO agentBaseDefDao) {
		this.agentBaseDefDao = agentBaseDefDao;
	}

	public String getById() throws Exception {
		String userName = MySecurityContextHandle.getUserName();
		if (StringUtils.isEmpty(userName))
			throw new ServiceException("请先登录");
		AgentBaseConfig agentBaseConfig = agentBaseConfigDao.findById(userName);
		Integer agentBase = null;
		if (null != agentBaseConfig) {
			agentBase = agentBaseConfig.getAgentBase();
		}
		if (null == agentBase || agentBase.intValue() < 1) {
			List<AgentBaseDef> agentBaseDefL = agentBaseDefDao.findAll(null);
			agentBase = agentBaseDefL.get(0).getAgentBase();
		}
		if (null == agentBase || agentBase.intValue() < 1) {
			agentBase = 600;
		}
		return agentBase.toString();
	}

	public String add(AgentBaseConfig model) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if(!"admin".equals(userName))
			throw new ServiceException("请先登录");
		// 验证
		String agentName = model.getAgentName();
		if (StringUtils.isEmpty(agentName))
			throw new ServiceException("代理商不能为空");
		List<IfwAgentRegInfo> agentRegInfoL = agentRegInfoDao.findByProperties(
				new String[] { "agentName" }, new Object[] { agentName });
		if (null == agentRegInfoL || agentRegInfoL.isEmpty())
			throw new ServiceException("代理商不存在");
		AgentBaseConfig agentBaseConfig = agentBaseConfigDao
				.findById(agentName);
		if (null != agentBaseConfig)
			throw new ServiceException("代理商基数配置已存在");
		Integer agentBase = model.getAgentBase();
		if (null == agentBase)
			agentBase = 0;
		if (agentBase < 1)
			throw new ServiceException("基数必须大于0");
		agentBaseConfigDao.save(model);
		return "1";
	}

	public String edit(AgentBaseConfig model) throws Exception {
		// 验证
		String userName = MySecurityContextHandle.getUserName();
		if(!"admin".equals(userName))
			throw new ServiceException("请先登录");
		// 验证
		String agentName = model.getAgentName();
		if (StringUtils.isEmpty(agentName))
			throw new ServiceException("代理商不能为空");
		List<IfwAgentRegInfo> agentRegInfoL = agentRegInfoDao.findByProperties(
				new String[] { "agentName" }, new Object[] { agentName });
		if (null == agentRegInfoL || agentRegInfoL.isEmpty())
			throw new ServiceException("代理商不存在");
		AgentBaseConfig agentBaseConfig = agentBaseConfigDao
				.findById(agentName);
		if (null == agentBaseConfig)
			throw new ServiceException("代理商基数配置不存在");
		Integer agentBase = model.getAgentBase();
		if (null == agentBase)
			agentBase = 0;
		if (agentBase < 1)
			throw new ServiceException("基数必须大于0");
		agentBaseConfig.setAgentBase(agentBase);
		agentBaseConfig.setNote(model.getNote());
		agentBaseConfigDao.update(model);
		return "1";
	}

}
